package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.OrderDeliveryLabel;
import com.wing.order.model.response.*;
import com.wing.order.service.OrderDeliveryLabelService;
import com.wing.order.service.OrderDeliverySignService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "Admin-签收物资入库管理", tags = "#/order/orderDeliverySignStorage")
@RestController("orderDeliverySignStorageController")
@RequestMapping("order/admin/orderDeliverySignStorage")
@RequiredArgsConstructor
public class OrderDeliverySignStorageController {

    private final OrderDeliverySignService orderDeliverySignService;
    @Autowired
    private OrderDeliveryLabelService orderDeliveryLabelService;


    @ApiOperation(value = "查询签收物资入库列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "签收记录编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderDeliverySignVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,OrderDeliverySignVO orderDeliverySignVO
    ){

        IPage<OrderDeliverySignVO> list = orderDeliverySignService.getOrderDeliverySignVOPageList(page,limit,orderDeliverySignVO);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询签收物资入库详情列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "signId", value = "签收记录id", defaultValue = "", example = "", required = true,dataType = "Long", paramType = "query")
    })
    @GetMapping("/getSignStorageDetailList")
    public PageResult<OrderDeliverySignDetailVO> getSignStorageDetailList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long signId
    ){

        IPage<OrderDeliverySignDetailVO> list = orderDeliverySignService.getSignStorageDetailList(page,limit,signId);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询料箱数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "料箱编号", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/getWorkbinByCode")
    public JsonResult<WorkbinVO> getWorkbinByCode(String code){
        WorkbinVO workbin = orderDeliverySignService.getWorkbinByCode(code);
        return JsonResult.success(workbin);
    }
    @ApiOperation(value = "查询托盘存放物资列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boxId", value = "料箱id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getMaterialVOListById")
    public JsonResult<List<MaterialVO>> getMaterialVOListById(Long boxId){
        List<MaterialVO> list = orderDeliverySignService.getMaterialVOListById(boxId);
        return JsonResult.success(list);
    }

    /**
     * 查询产品的料箱
     * @param deliveryDetailId
     * @return
     */
    @ApiOperation(value = "查询产品料箱", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deliveryDetailId", value="配送单明细ID", dataType = "Long",  required = false,paramType = "query")
    })
    @GetMapping("/getProductBoxByDetailId")
    public JsonResult<List<WorkbinVO>> getProductBoxByDetailId(Long deliveryDetailId){
        List<WorkbinVO> list = orderDeliverySignService.getProductBoxByDetailId(deliveryDetailId);
        if(CollectionUtils.isEmpty(list)){
            return JsonResult.fail("此产品未放入料箱，请先扫描包装条码入库");
        }
        return JsonResult.success(list);
    }

    /**
     * 查询料箱的包装码
     * @param boxId
     * @return
     */
    @ApiOperation(value = "查询料箱的包装码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boxId", value = "料箱id", defaultValue = "", example = "",required = true, dataType = "Integer", paramType = "query")

    })
    @GetMapping("/getDeliveryLabelVOByBoxId")
    public JsonResult<List<OrderDeliveryLabelVO>> getDeliveryLabelVOByBoxId(
            @RequestParam(value = "boxId",required = true) Long boxId

    ){
        List<OrderDeliveryLabelVO> list = orderDeliverySignService.getDeliveryLabelVOByBoxId(boxId);
        if(CollectionUtils.isEmpty(list)){
            return JsonResult.fail("未找到料箱的包装条码");
        }
        return JsonResult.success(list);

    }

    /**
     * 签收物资入库扫描包装编码
     * @param packLabelCode
     * @param signId
     * @return
     */
    @ApiOperation(value = "签收物资入库扫描包装编码", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "packLabelCode", value = "包装编码", defaultValue = "", example = "",required = true, dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "signId", value = "签收记录id", defaultValue = "", example = "",required = true, dataType = "Integer", paramType = "query")

    })
    @GetMapping("/getDeliveryLabelVOByCode")
    public JsonResult<OrderDeliveryLabelVO> getDeliveryLabelVOByCode(
            @RequestParam(value = "packLabelCode",required = true) String packLabelCode
            ,@RequestParam(value = "signId",required = true) Long signId

    ){
        OrderDeliveryLabelVO deliveryLabelVO = orderDeliverySignService.getDeliveryLabelVOByCode(packLabelCode, signId);
        if(deliveryLabelVO != null){
            if(deliveryLabelVO.getStatus().getValue() == 2){
                return JsonResult.fail("包装条码已入库");
            }
            return JsonResult.success(deliveryLabelVO);
        }else{
            return JsonResult.fail("未找到包装条码");
        }
    }
    @ApiOperation(value = "签收物资入库提交")
    @PostMapping("/inventoryInSubmit")
    public JsonResult inventoryImSubmit(@RequestBody List<OrderDeliveryLabel> list, HttpServletRequest request){
        try{
            Long creator= JwtUtils.getLoginUserId();
            orderDeliveryLabelService.inventoryInSubmit(list);
            return JsonResult.success("签收物资成功");
        }catch (Exception e){
            log.error("receive submit fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }
}
